<template>
  <div>
    <!-- <p>js 把字符串保存为txt文件，并下载到本地</p> -->
  </div>
</template>
<script>
export default {
  mounted() {
    this.$bus.$on("downloadStr", (name, data) => {
      this.exportRaw(name, data);
    });
  },
  beforeDestroy() {
    this.$bus.$off("downloadStr");
  },
  methods: {
    handleClick() {
      this.exportRaw("text.txt", "123123123");
    },
    fakeClick(obj) {
      var ev = document.createEvent("MouseEvents");
      ev.initMouseEvent(
        "click",
        true,
        false,
        window,
        0,
        0,
        0,
        0,
        0,
        false,
        false,
        false,
        false,
        0,
        null
      );
      obj.dispatchEvent(ev);
    },

    exportRaw(name, data) {
      var urlObject = window.URL || window.webkitURL || window;
      var export_blob = new Blob([data]);
      var save_link = document.createElementNS(
        "http://www.w3.org/1999/xhtml",
        "a"
      );
      save_link.href = urlObject.createObjectURL(export_blob);
      save_link.download = name;
      this.fakeClick(save_link);
    },
  },
};
</script>
<style scoped lang="scss">
</style>